#include <iostream>
#include <cmath>
using namespace std;

int reversiblePrimeWithRadix(int n,int radix){
	char source[32];
	int i = 0;
	while( n ){
		source[i++] = n%radix;
		n /= radix;
	}
	int sum = 0;
	int power = 1;
	for(int j=i-1; j>-1; j--){
		sum += power*source[j];
		power *= radix;
	}
	return sum;
}
bool isPrime(int n){
	if( n == 1 ) return false;
	for(int i=2; i<=(int)sqrt(n); i++){
		if( n%i==0 ) return false;
	}
	return true;
}
int main(){
	int n,d;
	cin >> n;
	while( n>0 ){
		cin >> d;
		if( isPrime(n) && isPrime(reversiblePrimeWithRadix(n,d)) ) 
			cout << "Yes" << endl;
		else cout << "No" << endl;
		cin >> n;
	}
}
